import express from 'express'
import { get } from 'https'

const app = express()
const location = {
  hash: "",
  host: "www.jianshu.com",
  hostname: "www.jianshu.com",
  href: "https://www.jianshu.com/",
  origin: "https://www.jianshu.com",
  pathname: "/",
  port: "",
  protocol: "https:",
}

app.get('/', async (req, res, next) => {
  const handle = get(location.origin, response => {
    if (response.statusCode !== 200) {
      console.log('statusCode:', response.statusCode)
      return
    }
    const data = []
    response.setEncoding('utf-8')
    response.on('data', chunk => {
      data.push(chunk)
    })
    response.on('end', () => {
      const result = Buffer.concat(data).toString()
      console.log('result:\n', result)
    })
  })
  handle.on('error', err => {
    console.log('Error:\n ', err)
  })

  next()
})

app.listen(3000, () => { console.log(`Server run at 3000 port.`) })
